NumPy(二):NumPy创建数组的几种方式np.zeros()/ np.ones() /np.arange()/ np.linspace()/ np.indices()

您所在的位置:网站首页 詹妮弗洛佩兹的on the floor NumPy(二):NumPy创建数组的几种方式np.zeros()/ np.ones() /np.arange()/ np.linspace()/ np.indices()

NumPy(二):NumPy创建数组的几种方式np.zeros()/ np.ones() /np.arange()/ np.linspace()/ np.indices()

2024-02-25 13:53| 来源: 网络整理| 查看: 265

indices()返回一个给定形状数组的序号网格索引数组,可以用于提取数组元素或对数组进行切片使用。

语法:np.indices(dimensions, dtype, sparse=False)dimensions:网格的形状(整数序列)dtype:数据类型sparse:返回网格的稀疏表示,而不是密集表示。默认值是False。sparse=False时,返回一个网格索引数组grid.shape = (len(dimensions), ) + tuple(dimensions)。sparse=True时,返回数组的元素 import numpy as np x = np.arange(20).reshape((5,4)) dense_grid = np.indices((2,3)) #返回一个2x3网格序列,密集分布,每个行号和列号一一对应,表示一个位置的元素。 sparse_grid = np.indices((2,3),sparse= True) #返回一个松散排布的2x3网格的行分布和列分布元组,行号和列号不是一一对应,一个行号对应多个列号。 # 显示错误:indices() got an unexpected keyword argument 'sparse' # 原因可能是numpy版本问题,待更新后再尝试 print("x:\n",x) print("x.shape:",x.shape)

参考文章:

https://blog.csdn.net/daimashiren/article/details/111127432

https://vimsky.com/examples/usage/numpy-indices-function-python.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3